package com.shujia.flink.tf

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow

object Demo6Window {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val ds: DataStream[String] = env.socketTextStream("master", 8888)


    val kvDS: DataStream[(String, Int)] = ds.flatMap(_.split(""))
      .map((_, 1))


    val kyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)


    //5秒一个窗口
    val winDS: WindowedStream[(String, Int), String, TimeWindow] = kyByDS.timeWindow(Time.seconds(5))

    //开窗之后求和
    val countDS: DataStream[(String, Int)] = winDS.sum(1)


    countDS.print()

    env.execute()


  }

}
